package com.raymond.sgip.codec.encodeing;

import com.raymond.client.msg.BaseMessage;
import com.raymond.client.utils.NettyByteBufUtil;
import com.raymond.cmpp.utils.CommonUtil;
import com.raymond.sgip.constants.SgipConstants;
import com.raymond.sgip.msg.request.SubmitRequestMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.StandardCharsets;

/**
 * 短信提交请求编码
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-08-05 18:01
 */
public class SubmitRequestMessageEncoder extends BaseSgipEncoder {

    @Override
    public byte[] encode0(BaseMessage msg) throws Exception {
        SubmitRequestMessage requestMessage = (SubmitRequestMessage) msg;
        ByteBuf bodyBuffer = Unpooled.buffer();
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getSpNumber().
                getBytes(SgipConstants.DEFAULT_TRANSPORT_CHARSET), 21));
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getChargeNumber().
                getBytes(SgipConstants.DEFAULT_TRANSPORT_CHARSET), 21));
        int userCount = requestMessage.getUserCount();
        bodyBuffer.writeByte(userCount);
        for (int i = 0; i < userCount; i++) {
            String[] destTermId = requestMessage.getUserNumber();
            bodyBuffer.writeBytes(CommonUtil.ensureLength(destTermId[i].getBytes(SgipConstants.DEFAULT_TRANSPORT_CHARSET),
                    21));
        }

        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getCorpId().
                getBytes(SgipConstants.DEFAULT_TRANSPORT_CHARSET), 5));
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getServiceType().
                getBytes(SgipConstants.DEFAULT_TRANSPORT_CHARSET), 10));
        bodyBuffer.writeByte(requestMessage.getFeeType());
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getFeeValue().
                getBytes(SgipConstants.DEFAULT_TRANSPORT_CHARSET), 6));
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getGivenValue().
                getBytes(SgipConstants.DEFAULT_TRANSPORT_CHARSET), 6));
        bodyBuffer.writeByte(requestMessage.getAgentFlag());
        bodyBuffer.writeByte(requestMessage.getMorelateToMtFlag());
        bodyBuffer.writeByte(requestMessage.getPriority());
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getExpireTime().
                getBytes(SgipConstants.DEFAULT_TRANSPORT_CHARSET), 16));
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getScheduleTime().
                getBytes(SgipConstants.DEFAULT_TRANSPORT_CHARSET), 16));
        bodyBuffer.writeByte(requestMessage.getReportFlag());
        bodyBuffer.writeByte(requestMessage.getTpPid());
        bodyBuffer.writeByte(requestMessage.getTpUdhi());
        bodyBuffer.writeByte(requestMessage.getMessageCoding());
        bodyBuffer.writeByte(requestMessage.getMessageType());
        byte[] bytes = requestMessage.getMsgContent().getBytes(StandardCharsets.US_ASCII);
        bodyBuffer.writeInt(bytes.length);

        bodyBuffer.writeBytes(bytes);
        bodyBuffer.writeBytes(CommonUtil.ensureLength(requestMessage.getReserve().getBytes
                (SgipConstants.DEFAULT_TRANSPORT_CHARSET), 8));

        return NettyByteBufUtil.toArray(bodyBuffer, bodyBuffer.readableBytes());
    }
}
